<?php
/**
 * 权限类
 */
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Auths;
class Auth extends Common{

	/**
	 * 展示权限列表页
	 */
	public function lst(){
		
		//分配模版标头信息变量
		$this->setPageTitle("权限列表","添加权限",url('addShow'));

		//获取权限信息
		$authModel = new Auths;
		$auths = $authModel->select();
		$auths = collection($auths)->toArray();
		$auths = getTree($auths,'aid','parent_id',0);

		//分配变量
		$this->assign('auths',$auths);

		//展示视图
		return view();
	}

	/**
	 * 展示权限添加页
	 */
	public function addShow(){

		//分配模版标头信息变量
		$this->setPageTitle("添加权限","权限列表",url('lst'));

		//获取权限信息
		$authModel = new Auths;
		$auths = $authModel->field('aid,auth_name,parent_id,auth_level')->select();
		$auths = collection($auths)->toArray();
		
		$auths = getTree($auths,'aid','parent_id',0);

		//分配变量
		$this->assign('auths',$auths);

		//展示视图
		return view('add');
	}

	/**
	 * 添加权限
	 */
	public function add(){

		//安全处理
		if(!request()->isPost()){

			$this->error('你访问的页面不存在！');

		}

		//验证数据格式
		$data = input('post.');
		$validate = new \app\admin\validate\Auths();
		if(!$validate->check($data)){

			$this->error($validate->getError());

		}

		//处理数据
		$auth = new Auths();
		$auth->auth_name=$data['pri_name'];
		$auth->controler_name=ucfirst($data['ctl_name']);
		$auth->action_name=$data['act_name'];
		$auth->parent_id=$data['pid']+0;
		$auth->auth_level=$data['level']+0;
		
		//添加数据
		if(false!==$auth->allowField(true)->save()){

			//获取最后执行的sql语句
			$sql = Auths::getLastSql();

			//添加管理员日志
			adminLog( session('id'), '添加了权限：'.$data['pri_name'], $sql );

			$this->success('添加成功，刷新页面后才能看到新权限！',url('lst'));

		}else{

			$this->error('添加失败');
		}

	}

	/**
	 * 展示权限修改页
	 */
	public function editShow(){

		//分配模版标头信息变量
		$this->setPageTitle("修改权限","权限列表",url('lst'));

		//获取权限信息
		$authModel = new Auths;
		$auths = $authModel->field('aid,auth_name,parent_id,auth_level')->select();
		$auths = collection($auths)->toArray();		
		$auths = getTree($auths,'aid','parent_id',0);

		//获取要修改的权限信息
		$editAuth = $authModel->find(input('id'))->toArray();

		//分配变量
		$this->assign('auths',$auths);
		$this->assign('editAuth',$editAuth);

		//展示视图
		return view('edit');
	}

	/**
	 * 修改权限
	 */
	public function edit(){
		
		//安全处理
		if(!request()->isPost()){

			$this->error('你访问的页面不存在！');

		}

		//验证数据格式
		$data = input('post.');
		$validate = new \app\admin\validate\Auths();
		if(!$validate->check($data)){

			$this->error($validate->getError());

		}

		//处理数据
		$auth = new Auths();
		$auth->auth_name=$data['pri_name'];
		$auth->controler_name=ucfirst($data['ctl_name']);
		$auth->action_name=$data['act_name'];
		$auth->parent_id=$data['pid']+0;
		$auth->auth_level=$data['level']+0;
		$auth->aid=$data['auth_id']+0;

		//如果父级id是自己的下级或自己就不允许更新
		$ids = $sonIds = $auth->where( 'parent_id','=',$auth->aid )->column('aid');//获取子集id
		$ids[]=$auth->aid;//将自身id放入
		if( in_array( $auth->parent_id, $ids ) ){
			$this->error('不能将自身或子集作为父级权限！');
		}

		//添加数据
		if( false!==$auth->isUpdate(true)->save() ) {

			//更新权限级别字段
			if( $data['pLevel']+0 != $auth->parent_id ) {

				$level = 0;
				$sonLevel = 1;

				if( $auth->parent_id ) {

					//父级权限等级深度
					$pLevel = $auth->where( 'aid','=',$auth->parent_id )->value('auth_level');

					$level = $pLevel+1;
					$sonLevel = $pLevel+2;

				}				
				
				//拼凑更新数据
				$current = ['aid'=>$auth->aid,'auth_level'=>$level];

				//更新当前权限等级深度
				$db = db('auth');
				$db->strict(true)->update( $current );

				//如果有子集
				if( $sonIds ) {

					//拼凑更新数据
					$son = ['auth_level'=>$sonLevel];

					//更新当前权限等级深度
					$auth->where(['aid'=>['in',$sonIds]])->strict(true)->update($son);
					
				}

			}

			//获取最后执行的sql语句
			$sql = Auths::getLastSql();

			//添加管理员日志
			adminLog( session('id'), '更新了权限：'.$data['pri_name'], $sql );

			$this->success('更新成功',url('lst'));

		}else {

			$this->error('更新失败');
		}

	}

	/**
	 * 异步删除权限
	 */
	public function del() {

		//安全处理
		if(!request()->isAjax()) {

			$this->error('你访问的页面不存在！');

		}

		//判断是否有子权限
		$id = input('id')+0;
		$auth = new \app\admin\model\Auths();

		if($auth->where('parent_id = '.$id)->count()){

			echo json_encode(['status'=>0,'msg'=>'该权限下存在子权限，请先删除子权限！']);
			die;
		}

		//删除权限
		if($auth->destroy($id)) {

			//获取最后执行的sql语句
			$sql = Auths::getLastSql();

			//添加管理员日志
			adminLog( session('id'), '删除了权限：'.input('name'), $sql );

			echo json_encode(['status'=>1,'msg'=>'删除成功']);
		
		}else {

			echo json_encode(['status'=>0,'msg'=>'删除失败']);

		}

	}




}